﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Icinetic.FeedCategorySample.Entities;
using Icinetic.FeedCategorySample.Services.Repositories;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;


namespace Icinetic.FeedCategorySample.Services.Test
{
    [TestClass]
    public class RssItemRepositoryTest
    {
        private string dbPath = RssItemRepository.DbPath;

        [TestInitialize]
        public void TestInitialize()
        {
            RssItemRepository.DbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "FeedCategorySampleTestDB.db");
            RssItemRepository.Initialize();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            RssItemRepository.DbPath = dbPath;
        }

        [TestMethod]
        public void AddTest()
        {
            RssItem rssItem = NewRssItem();
            RssItemRepository rssItemService = new RssItemRepository();

            rssItemService.Add(rssItem);

            Assert.IsTrue(rssItem.OID > 0);
        }

        [TestMethod]
        public void AddListTest()
        {
            List<RssItem> rssItemList = NewRssItemList();
            RssItemRepository rssItemService = new RssItemRepository();

            rssItemService.Add(rssItemList);

            Assert.IsFalse(rssItemList.Any(rssItem => rssItem.OID < 1));
        }

        [TestMethod]
        public void ChangeTest()
        {
            RssItem rssItem = NewRssItem();
            RssItemRepository rssItemService = new RssItemRepository();
            rssItemService.Add(rssItem);
            rssItem = rssItemService.Find(rssItem.OID);

            string content = Guid.NewGuid().ToString();
            rssItem.Content = content;
            rssItemService.Change(rssItem);

            rssItem = rssItemService.Find(rssItem.OID);
            Assert.AreEqual(rssItem.Content, content);
        }

        [TestMethod]
        public void RemoveTest()
        {
            RssItem rssItem = NewRssItem();
            RssItemRepository rssItemService = new RssItemRepository();
            rssItemService.Add(rssItem);
            rssItem = rssItemService.Find(rssItem.OID);

            rssItemService.Remove(rssItem);

            rssItem = rssItemService.Find(rssItem.OID);
            Assert.IsNull(rssItem);
        }

        [TestMethod]
        public void FindTest()
        {
            RssItem rssItem = NewRssItem();
            RssItemRepository rssItemService = new RssItemRepository();
            rssItemService.Add(rssItem);

            rssItem = rssItemService.Find(rssItem.OID);

            Assert.IsNotNull(rssItem);
        }

        [TestMethod]
        public void FindAllTest()
        {
            RssItem rssItem = NewRssItem();
            RssItemRepository rssItemService = new RssItemRepository();
            rssItemService.Add(rssItem);

            IList<RssItem> rssItemList = rssItemService.FindAll();

            Assert.IsTrue(rssItemList.Count > 0);
        }

        [TestMethod]
        public void SearchTest()
        {
            RssItem rssItem = NewRssItem();
            RssItemRepository rssItemService = new RssItemRepository();
            rssItemService.Add(rssItem);

            IList<RssItem> rssItemList = rssItemService.Search(item => item.OID == rssItem.OID, null);

            Assert.IsTrue(rssItemList.Count > 0);
        }

        [TestMethod]
        public void SearchPagedTest()
        {
            List<RssItem> rssItemList = NewRssItemList();
            RssItemRepository rssItemService = new RssItemRepository();
            rssItemService.Add(rssItemList);

            rssItemList = rssItemService.Search(item => item.Title.Contains("Rss"), null, false, 1, 5).ToList();

            Assert.IsTrue(rssItemList.Count == 5);
        }

        [TestMethod]
        public void SearchCountTest()
        {
            List<RssItem> rssItemList = NewRssItemList();
            RssItemRepository rssItemService = new RssItemRepository();
            rssItemService.Add(rssItemList);

            int count = rssItemService.SearchCount(item => item.Title.Contains("5"));

            Assert.IsTrue(count > 0);
        }

        private RssItem NewRssItem(int index = 1) 
        {
            DateTime now = DateTime.Now;
            return new RssItem
            {
                Category = "TestCategory",
                ColPortraitSpan = index,
                RowPortraitSpan = index,
                ColSpan = index,
                RowSpan = index,
                Content = Guid.NewGuid().ToString(),
                Date = now.ToString(),
                DateArticle = now,
                ExpirationDate = now,
                Image = "ImageTest",
                IsRead = false,
                Link = "TestLink",
                Title = "RssItem " + index.ToString(),
                TitleAll = "RssItem " + index.ToString()
            };
        }

        private List<RssItem> NewRssItemList()
        {
            List<RssItem> rssItemList = new List<RssItem>();
            for (int i = 0; i < 10; i++)
            {
                rssItemList.Add(NewRssItem(i));
            }
            return rssItemList;
        }
    }
}
